package com.zhiyou100.interceptor;

import cn.hutool.core.lang.Console;
import cn.hutool.core.util.ObjectUtil;
import com.zhiyou100.exception.LoginException;
import com.zhiyou100.service.TeacherService;
import lombok.experimental.var;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 用户登陆拦截器
 *
 * @author yang
 * @version 1.0.0
 * @date 2020-10-02 22:14
 */
public class UserLoginInterceptor implements HandlerInterceptor {


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Console.log(" 用户登陆拦截器启动！ ");
        String userSessionName = "teacher";
        if (ObjectUtil.isNotNull(request.getSession().getAttribute(userSessionName))) {
            // 登陆了，放行
            return true;
        } else {
            // 没有登陆
            throw new LoginException("必须登陆才能请求 " + request.getServletPath());
        }
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
